package com.dongdong.bookstore.module.cms.service.impl;

import com.dongdong.bookstore.common.service.RedisService;
import com.dongdong.bookstore.module.cms.model.User;
import com.dongdong.bookstore.module.cms.service.CmsCacheService;
import com.dongdong.bookstore.module.cms.service.UserService;
import com.dongdong.bookstore.module.ums.model.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author 林泉梁
 * @version 1.0
 */
@Service
public class CmsCacheServiceImpl implements CmsCacheService {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;

    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.key.cms.user}")
    private String REDIS_KEY_USER;



    @Override
    public void delUser(Long userId) {
        User user = userService.getById(userId);
        if(user != null){
            String key = REDIS_DATABASE + ":" + REDIS_KEY_USER+ ":" + user.getUsername();
            redisService.del(key);
        }
    }

    @Override
    public User getUser(String username) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_USER+ ":" + username;
        return (User)redisService.get(key);
    }

    @Override
    public void setUser(User user) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_USER+ ":" + user.getUsername();
        redisService.set(key,user,REDIS_EXPIRE);
    }
}
